package com.lemei.cn.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.lemei.cn.adpter.TestArrayAdapter;
import com.lemei.cn.lemeiapp.BasicActivity;
import com.lemei.cn.lemeiapp.LeMei;
import com.lemei.cn.lemeiapp.R;
import com.lemei.cn.utils.CleanUtiles;
import com.lemei.cn.utils.ToastMake;

/**
 * Created by 赵航 on 2017/5/26.
 */

public class LongInActivity extends BasicActivity implements View.OnClickListener {

    private EditText mSeclceText;
    private EditText mName;
    private EditText mMiMa;
    private Spinner  mSpinner;
    private String   mLongin_mima;
    private String   mLongin_name;
    private String   mSelcet_choice;
    private Button   mLongin;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_main);
        initView();
        initSpinner();

    }

    private void initView() {
        mSeclceText = (EditText) findViewById(R.id.longin_seclect_edit);
        mName = (EditText) findViewById(R.id.iongin_name);
        mMiMa = (EditText) findViewById(R.id.iongin_mima);
        mSpinner = (Spinner) findViewById(R.id.longin_select);
        mTextView = (TextView) findViewById(R.id.longin_zhuce);

        mLongin = (Button) findViewById(R.id.longin_longin);
        mLongin_mima = mMiMa.getText().toString().trim();
        mLongin_name = mName.getText().toString().trim();
        mSelcet_choice = mSeclceText.getText().toString().trim();


        boolean doclean = CleanUtiles.doclean(mLongin_mima);
        if (doclean) {
            mSeclceText.setText("");
        }
        boolean doclean1 = CleanUtiles.doclean(mLongin_mima);
        if (doclean1) {
            mMiMa.getText().clear();
        }
        boolean doclean2 = CleanUtiles.doclean(mLongin_name);
        if (doclean2) {
            mName.setText("");
        }

        mTextView.setOnClickListener(this);
        mLongin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.longin_zhuce:
                startActivity(ZhuCe.class, false);

                break;
            case R.id.longin_longin:
                mLongin_mima = mMiMa.getText().toString().trim();
                mLongin_name = mName.getText().toString().trim();
                mSelcet_choice = mSeclceText.getText().toString().trim();
                if (TextUtils.equals("text", mLongin_name) && TextUtils.equals("123", mLongin_mima) && TextUtils.equals("ABC", mSelcet_choice)) {
                   showDialog("正在加载。。。。请稍等");

                    LeMei.mainHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    hideDialog();
                                    startActivity(MianActivity.class, true);
                                }
                            });

                        }
                    }, 3000);


                }else {
                    ToastMake.showToast("请输入正确的账号跟密码");
                }

                break;
        }

    }

    private void initSpinner() {
        final String[] object = new String[]{"请选择","ABC", "DEF", "GHK", "LMN", "OPQ"};
        mSpinner.setAdapter(new TestArrayAdapter(LeMei.context,object));
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        mSeclceText.setText(object[0]);
                        break;
                    case 1:
                        mSeclceText.setText(object[1]);
                        break;
                    case 2:
                        mSeclceText.setText(object[2]);
                        break;
                    case 3:
                        mSeclceText.setText(object[3]);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    @Override
    public void onBackPressed() {
        //  super.onBackPressed();
        MaterialDialog.Builder builder = new MaterialDialog.Builder(LongInActivity.this);
        builder.title("退出提示");
        builder.content("亲，确定没点错");
        builder.negativeText("点错了");
        builder.positiveText("确定");
        builder.show();
        builder.onNegative(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.dismiss();
            }
        });
        builder.onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                dialog.dismiss();
                finish();
            }
        });


    }

}
